package com.wwwarehouse.usercenter.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CodeEditText;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.receiver.SmsReceiver;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jackie on 2017/6/27.
 * 忘记密码
 */

public class ForgetPasswordStep2Activity extends BaseActivity implements View.OnClickListener {
    private View mRootView;

    private ImageView mBackImg;
    private TextView mTitleTxt;

    private TextView mDescriptionTxt;
    private CodeEditText mCodeEdt;
    private TextView mGetCodeTxt;

    private String mMobile;
    private String mSMSCode;

    private CountDownTimer mCountDownTimer;

    private SmsReceiver mSmsReceiver;

    private static final int REQUEST_GET_SMS_CODE = 0;
    private static final int REQUEST_CHECK_SMS_CODE = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_forget_password_2);

        if (getIntent() != null) {
            mMobile = getIntent().getStringExtra(UserCenterConstant.KEY_LOGIN_MOBILE);
        }

        initView();
        initEvent();
    }

    private void initView() {
        mRootView = findViewById(R.id.rl_root);

        mBackImg = findView(R.id.back_bt);
        mTitleTxt = findView(R.id.title_text);
        mTitleTxt.setText(R.string.sms_check);

        mDescriptionTxt = findView(R.id.tv_description);
        //括号表示 组  被替换的部分   $n 表示 第n组的内容
        String description = String.format(getString(R.string.forget_password_description), "+86 " + mMobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        mDescriptionTxt.setText(description);

        mCodeEdt = findView(R.id.et_code);
        mGetCodeTxt = findView(R.id.tv_get_code);

        mCountDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mGetCodeTxt.setClickable(false); //设置不可点击
                mGetCodeTxt.setBackgroundResource(R.drawable.common_get_sms_code_shape_press);
                mGetCodeTxt.setTextColor(getResources().getColor(R.color.common_color_c8_d5d7dc));
                mGetCodeTxt.setText(String.format(getResources().getString(R.string.get_code_time), millisUntilFinished / 1000 + "s"));  //设置倒计时时间
            }

            @Override
            public void onFinish() {
                mGetCodeTxt.setClickable(true);
                mGetCodeTxt.setBackgroundResource(R.drawable.common_get_sms_code_shape_normal);
                mGetCodeTxt.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
                mGetCodeTxt.setText(R.string.get_code);
            }
        };

        mCountDownTimer.start();

        mSmsReceiver = new SmsReceiver();
    }

    private void initEvent() {
        mRootView.setOnClickListener(this);

        mBackImg.setOnClickListener(this);

        mCodeEdt.setOnInputFinishedListener(new CodeEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String code) {
                mSMSCode = code;

                //验证短信验证码
                checkSMSCode();
            }
        });

        mGetCodeTxt.setOnClickListener(this);

        mSmsReceiver.setOnMessageReceivedListener(new SmsReceiver.OnMessageReceivedListener() {
            @Override
            public void onReceived(String code) {
                mCodeEdt.setText(code);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.rl_root) {
            hideSoftKeyBoard(mRootView);
        } else if (id == R.id.back_bt) {
            finishActivity();
        } else if (id == R.id.tv_get_code) {
            generateSMSCode();
        }
    }

    /**
     * 获取手机短信验证码
     */
    private void generateSMSCode() {
        Map map = new HashMap();
        map.put("mobile", mMobile);
        map.put("smsType", "3");

        NoHttpUtils.httpPost(UserCenterConstant.URL_GET_SMS_VERIFICATION, map, mOnResponseListener, REQUEST_GET_SMS_CODE);
    }

    /**
     * 验证手机短信验证码
     */
    private void checkSMSCode() {
        Map map = new HashMap();
        map.put("mobile", mMobile);
        map.put("verifycode", mSMSCode);

        NoHttpUtils.httpPost(UserCenterConstant.URL_CHECK_VERIFY_CODE, map, mOnResponseListener, REQUEST_CHECK_SMS_CODE);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_SMS_CODE:
                    if ("0".equals(commonClass.getCode())) {
                        //获取短信验证码成功
                        mCountDownTimer.start();
                    }  else if ("70003".equals(commonClass.getCode())) {  //70003 验证码错误
                        mCodeEdt.setStateWrong();

                        toast(commonClass.getMsg());
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_CHECK_SMS_CODE:
                    if ("0".equals(commonClass.getCode())) {
                        //短信验证码验证通过
                        Bundle bundle = new Bundle();
                        bundle.putString(UserCenterConstant.KEY_LOGIN_MOBILE, mMobile);
                        bundle.putString(UserCenterConstant.KEY_LOGIN_SMS_CODE, mSMSCode);
                        startActivity(ForgetPasswordStep3Activity.class, bundle, true);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {

        }
    };
}
